package com.mrd.serverconnect.pkg;

import android.util.Log;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Created by liushenen on 2018/1/31.
 */
public class AuthPKGHelp extends PKGBase {
    private static final String TAG = "AuthPKGHelp";

    public AuthReqData readReqData(byte[] head, byte funCode, DataInputStream dataInputStream) {
        AuthReqData mAuthReqData = new AuthReqData();
        try {
            byte[] sn = new byte[16];
            int version = dataInputStream.readInt();
            dataInputStream.read(sn);
            mAuthReqData.head = head;
            mAuthReqData.funCode = funCode;
            mAuthReqData.sn = sn;
            mAuthReqData.version = version;
            Log.i(TAG, "设备鉴权请求 =================");
            Log.i(TAG, "设备版本：" + version);
            Log.i(TAG, "设备号:" + new String(sn, 0, 13, "utf-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return mAuthReqData;
    }

    /**
     * @param lastRecordTime 本地记录最近更新客流的时间
     **/
    public void sendData(byte[] head, OutputStream outputStream, long lastRecordTime, byte[] sn) {
        DateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        defaultFormat.setTimeZone(TimeZone.getDefault());
        Log.i(TAG, "timezone: "+TimeZone.getDefault());
        try {
            long curTime = System.currentTimeMillis();
            //获取5分钟前的时间;
//            String upTime = defaultFormat.format(curTime - 5 * 60 * 1000);
//            //获取当前时间
//            String syncTime = defaultFormat.format(curTime);
            //校验本地时间与数据库最新记录时间,保证回写客流设备时间不低于数据库最新时间
            String upTime = defaultFormat.format(new Date(curTime - 5 * 60 * 1000));
            String syncTime = defaultFormat.format(new Date(curTime));
//            if (curTime < lastRecordTime) {
//
//                Log.i(TAG, "system time is error");
//            } else {
//                Log.i(TAG, "system time is ok");
//            }

            Log.i(TAG,"upTime：" + upTime);
            Log.i(TAG,"syncTime：" + syncTime);

            head[5] = 0x2e;
            outputStream.write(head);
            outputStream.write(0x44);
            outputStream.write(upTime.getBytes());
            outputStream.write(0x00);
            outputStream.write(syncTime.getBytes());
            outputStream.write(0x00);
            outputStream.write(0x00);
            outputStream.write(0x00);
            outputStream.write(0x00);
            outputStream.write(15);
            outputStream.flush();
        } catch (IOException e) {
            Log.e(TAG, " 鉴权-IOException" + e.getMessage());
        }
    }


    /**
     * 是否在线
     */
    private boolean online;

    public boolean isOnline() {
        return online;
    }

    public void setOnline(boolean online) {
        this.online = online;
    }

    /**
     * 本地局域网设备获取的ip
     */
    private String localIp;

    public String getLocalIp() {
        return localIp;
    }

    public void setLocalIp(String localIp) {
        this.localIp = localIp;
    }

    /**
     * M6 设备SN；
     */
    private String m6Sn;

    public String getM6Sn() {
        return m6Sn;
    }

    public void setM6Sn(String m6Sn) {
        this.m6Sn = m6Sn;
    }

    public class AuthReqData extends BaseReqData {
        int version; //版本号
        public byte[] sn;//sn号
    }

}
